package com.qifan.alb2b.dao;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Repository;

import com.cqcis.base.orm.jdbc.JdbcUtil;
import com.qifan.alb2b.action.back.param.UserQueryParam;
import com.qifan.alb2b.domain.HotelInfoDomain;
import com.qifan.alb2b.domain.OrderInfoDomain;
import com.qifan.alb2b.domain.UserInfoDomain;
import com.qifan.alb2b.util.Constants;
import com.qifan.alb2b.util.DataGridModel;
import com.qifan.alb2b.util.DateUtil;
import com.qifan.alb2b.util.Page;

/**
 * 用户CURD的Dao
 * @author lj
 *
 */
@Repository
public class UserInfoDao extends BaseDao{
	private static final Log logger=LogFactory.getLog(UserInfoDao.class);
//	private int update;
	
	/**
	 * 根据条件分页查询用户信息
	 * @param dgm 分页参数
	 * @param uqp 用户查询参数
	 * @return Page对象（分页信息&分页数据）
	 */
	public Page query(DataGridModel dgm,UserQueryParam uqp){
		logger.debug("查询用户");
		/* 构建sql */
		StringBuilder sql=new StringBuilder();
		List<Object> params=new ArrayList<Object>();
		sql.append("select * from userinfo where 1=1");
		if(uqp.getUserName()!=null&&!"".equals(uqp.getUserName())){
			sql.append(" and username like ?");
			params.add("%"+uqp.getUserName()+"%");
		}
		if(uqp.getUserAge()>-1){
			sql.append(" and userage=?");
			params.add(uqp.getUserAge());
		}
		if(uqp.getBeginBirth()!=null){
			sql.append(" and userbirth>=?");
			params.add(DateUtil.ymd(uqp.getBeginBirth()));
		}
		if(uqp.getEndBirth()!=null){
			sql.append(" and userbirth<=?");
			params.add(DateUtil.ymd(uqp.getEndBirth()));
		}
		if(uqp.getCreateDate()!=null){
			sql.append(" and createtime=?");
			params.add(DateUtil.ymd(uqp.getCreateDate()));
		}
		//return super.queryMapByPage(sql.toString(), dgm.getRows(), dgm.getPage(),params);
		return super.queryObjByPage(sql.toString(), dgm.getRows(), dgm.getPage(), params, UserInfoDomain.class);
	}
	
	public UserInfoDomain getUserById(String id){
		String sql="select * from userinfo where userid=?";
		List<UserInfoDomain> list= JdbcUtil.queryForList(this, UserInfoDomain.class, sql, id);
		if(list.size()>0)
			return list.get(0);
		return null;
	}
	public UserInfoDomain getUser(String userName,String password){
		String sql="select * from userinfo where username=? and userPwd =?";
		List<UserInfoDomain> list= JdbcUtil.queryForList(this, UserInfoDomain.class, sql, new String[]{userName,password});
		if(list.size()>0)
			return list.get(0);
		return null;
	}
	public UserInfoDomain getUserByUserName(String userName){
		String sql="select * from userinfo where username=?";
		List<UserInfoDomain> list= JdbcUtil.queryForList(this, UserInfoDomain.class, sql, userName);
		if(list.size()>0)
			return list.get(0);
		return null;
	}
	public List<OrderInfoDomain>  getOrderByUserId(String userId){
		String sql="select * from orderinfo where userId =?";
		List<OrderInfoDomain> list= JdbcUtil.queryForList(this, OrderInfoDomain.class, sql, userId);
		if(list.size()>0)
			return list;
		return null;
	}
	
	public UserInfoDomain getUserByCompId(String id){
		String sql="select * from userinfo where compid=?";
		List<UserInfoDomain> list= JdbcUtil.queryForList(this, UserInfoDomain.class, sql, id);
		if(list.size()>0)
			return list.get(0);
		return null;
	}
	
	
	/**
	 * 增加一个用户
	 * @param userInfoDomain
	 */
	public void add(UserInfoDomain userInfoDomain){
		super.addObj(userInfoDomain);
	}
	
	public void update(UserInfoDomain userInfoDomain){
		super.updateObj(userInfoDomain, "userId");
	}
	
	/**
	 * 批量删除
	 * @param ids
	 */
	public void delete(String[] ids){
		StringBuilder delSql=new StringBuilder();
		delSql.append("delete from userinfo where userid in(");
		for(int i=0;i<ids.length-1;i++){
			delSql.append("?,");
		}
		delSql.append("?)");
		this.getJdbcTemplate().update(delSql.toString(), ids);
	}
	/**
	 * 修改状态
	 * @param ids
	 */
	public void updateStatus(String[] ids,String status){
		
		StringBuilder delSql=new StringBuilder();
		delSql.append("update  userinfo set status ='"+status+"' where userid in(");
		for(int i=0;i<ids.length-1;i++){
			delSql.append("?,");
		}
		delSql.append("?)");
		
		this.getJdbcTemplate().update(delSql.toString(),ids);
	}
	
}
